package mvc

import (
	"os"
	"strings"
    "tpl"
)

var (
	rootPath   string
	tplBasedir string
	//Since the bug of package path, we need to decide the pathSep
	pathSep string
)

const (
	ROOT    = `~`
	PARENT  = `..`
	CURRENT = `.`
	URLSEP  = `/`
	BASE    = `view`
)

func init() {
	if strings.ToLower(os.Getenv("GOOS")) == "windows" {
		pathSep = `\`
	} else {
		pathSep = `/`
	}

	rootPath = getRootPath()
	tplBasedir = rootPath + BASE + pathSep
}

func view(data interface{}, controller string, action string) string {
	viewPath := tplBasedir + strings.ToLower(controller) + pathSep + strings.ToLower(action)

	return renderView(data, viewPath+".tpl")
}

func renderView(data interface{}, path string) string {
   tpl := &tpl.Template{BaseDir:tplBasedir}
   return tpl.Render(data, path)
}

func getRootPath() string {
	temp := strings.Split(os.Args[0], pathSep, -1)
	return strings.Join(temp[0:(len(temp)-1)], pathSep) + pathSep
}
